package com.dm.yyxr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dm.yyxr.mapper.SupplystatusMapper;
import com.dm.yyxr.pojo.Admin;
import com.dm.yyxr.pojo.Area;
import com.dm.yyxr.pojo.Community;
import com.dm.yyxr.pojo.Supplystatus;
import com.dm.yyxr.pojo.dto.SupplystatusDto;
import com.dm.yyxr.service.AdminService;
import com.dm.yyxr.service.AreaService;
import com.dm.yyxr.service.CommunityService;
import com.dm.yyxr.service.SupplystatusService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SupplystatusServiceImpl extends ServiceImpl<SupplystatusMapper, Supplystatus> implements SupplystatusService {
    @Autowired
    AdminService adminService;
    @Autowired
    AreaService areaService;
    @Autowired
    CommunityService communityService;

    /**
     * 获取供电审核表的分页数据
     * @param pageNo
     * @return
     */
    @Override
    public Page<SupplystatusDto> getSupplystatus(Integer pageNo) {
        //创建一个Page对象，指定当前页码和每页显示的记录数
        Page<Supplystatus> page = new Page<>(pageNo,5);
        //构建条件查询器
        LambdaQueryWrapper<Supplystatus> queryWrapper = new LambdaQueryWrapper<>();
        //根据Supplystatus对象的supplytime字段进行降序排序
        queryWrapper.orderByDesc(Supplystatus::getSupplytime);
        //调用selectPage方法执行查询操作，并将查询结果保存到分页对象page中
        page = baseMapper.selectPage(page, queryWrapper);
        //对分页对象中的记录进行操作，根据Supplystatus对象创建一个SupplystatusDto对象，并一并拷贝数据给SupplystatusDto
        // 根据admin_code、area_code和community_code查询相关实体对象，并设置到SupplystatusDto对象中。
        // 最后通过collect(Collectors.toList())转换为List<SupplystatusDto>
        List<SupplystatusDto> collect = page.getRecords().stream().map((supplystatus -> {
            SupplystatusDto supplystatusDto = new SupplystatusDto();
            BeanUtils.copyProperties(supplystatus, supplystatusDto);
            Admin admin = adminService.getById(supplystatus.getAdmin_code());
            Area area = areaService.getById(supplystatus.getArea_code());
            Community community = communityService.getById(supplystatus.getCommunity_code());
            supplystatusDto.setArea_name(area.getArea_name());
            supplystatusDto.setAdmin_name(admin.getAdmin_name());
            supplystatusDto.setCommunity_name(community.getCommunity_name());
            return supplystatusDto;
        })).collect(Collectors.toList());
        //创建一个新的Page对象res
        Page<SupplystatusDto> res = new Page<>();
        //将原分页对象page的属性值拷贝到新的分页对象res中
        BeanUtils.copyProperties(page,res);
        //将转换后的List<SupplystatusDto>设置为新分页对象res的记录列表
        res.setRecords(collect);
        return res;
    }

    /**
     * 设置供电审核表信息
     * @param supplystatus
     */
    @Override
    public void setSupplyStatus(Supplystatus supplystatus) {
        //将当前时间设置为供电审核表的approvaltime字段即审核时间
        supplystatus.setApprovaltime(new Date());
        //创建条件查询器
        LambdaQueryWrapper<Supplystatus> queryWrapper = new LambdaQueryWrapper<>();
        //创建一个SimpleDateFormat对象，设置日期格式为"yyyy-MM-dd"
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //根据supplystatus的logdate（供电时间）、admin_code字段、area_code字段、community_code字段和supply_num字段分别进行等值匹配
        queryWrapper.eq(Supplystatus::getLogdate,simpleDateFormat.format(supplystatus.getLogdate()))
                .eq(Supplystatus::getAdmin_code,supplystatus.getAdmin_code())
                .eq(Supplystatus::getArea_code,supplystatus.getArea_code())
                .eq(Supplystatus::getCommunity_code,supplystatus.getCommunity_code())
                .eq(Supplystatus::getSupply_num,supplystatus.getSupply_num());
        //更新操作，将supplystatus对象的属性值更新到满足条件的记录中
        baseMapper.update(supplystatus,queryWrapper);
    }
}
